package com.csun.cmny.provider.controller.admin;


import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.UaAppraiser;
import com.csun.cmny.provider.model.domain.UacEmp;
import com.csun.cmny.provider.model.domain.UacGroupRole;
import com.csun.cmny.provider.model.domain.UacUser;
import com.csun.cmny.provider.model.dto.appraiser.AppraiserAuditDto;
import com.csun.cmny.provider.model.dto.appraiser.AppraiserQueryDto;
import com.csun.cmny.provider.model.dto.appraiser.AppraiserSaveDto;
import com.csun.cmny.provider.model.dto.appraiser.AppraiserUpdateDto;
import com.csun.cmny.provider.model.dto.emp.UacEditEmpDto;
import com.csun.cmny.provider.model.enums.UacUserStatusEnum;
import com.csun.cmny.provider.model.vo.AppraiserQueryVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.UaAppraiserService;
import com.csun.cmny.provider.service.UacEmpService;
import com.csun.cmny.provider.service.UacGroupRoleService;
import com.csun.cmny.provider.service.UacUserService;
import com.csun.cmny.provider.utils.Md5Util;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/appraiser")
public class UacAppraiserController extends BaseController {

    @Resource
    private UaAppraiserService appraiserService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private UacEmpService empService;
    @Resource
    private UacGroupRoleService groupRoleService;
    @Resource
    private UacUserService userService;


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "新增评估师")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Wrapper addChanner(@RequestBody @Valid AppraiserSaveDto appraiserSaveDto, BindingResult br) {

        //校验此机构是否有评估师这个角色
        UacGroupRole uacGroupRole = new UacGroupRole();
        uacGroupRole.setGroupId(getLoginAuthDto().getGroupId());
        List<UacGroupRole> select = groupRoleService.select(uacGroupRole);
        int a=0;
        for (UacGroupRole groupRole : select) {
            if(groupRole.getRoleId().equals(22L)){
                a++;
            }
        }
        if(a<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("appraiser.group.role.error", null));
        }

        //校验手机号是否被注册评估师
        UacUser user = new UacUser();
        user.setMobileNo(appraiserSaveDto.getAppraiserPhone());
        List<UacUser> uacUsers = userService.select(user);
        for (UacUser uacUser : uacUsers) {
            UaAppraiser appraiser = new UaAppraiser();
            appraiser.setUserIdBind(uacUser.getId());
            List<UaAppraiser> uaAppraisers = appraiserService.select(appraiser);
            if(PublicUtil.isNotEmpty(uaAppraisers)){
                return WrapMapper.wrap(Wrapper.ERROR_CODE,cmnyMessageSource.getMessage("appraiser.phone.error", null));
            }
        }




        //新增员工
        UacEditEmpDto uacEditEmpDto = new UacEditEmpDto();
        uacEditEmpDto.setLoginName(appraiserSaveDto.getLoginName());
        uacEditEmpDto.setEmpDate(appraiserSaveDto.getEmpDate());
        uacEditEmpDto.setIdCard(appraiserSaveDto.getIdCard());
        uacEditEmpDto.setMobileNo(appraiserSaveDto.getAppraiserPhone());
        uacEditEmpDto.setDeptId(Long.parseLong(appraiserSaveDto.getAdministrative()));
        uacEditEmpDto.setRoleId(22L);
        uacEditEmpDto.setUserName(appraiserSaveDto.getAppraiserName());
        uacEditEmpDto.setPosition("评估师");
        empService.addUacEmpWithRole(uacEditEmpDto, getLoginAuthDto());

        /*UacUser uacUser = new UacUser();
        uacUser.setUserName(appraiserSaveDto.getAppraiserName());
        uacUser.setSex(appraiserSaveDto.getGender());
        uacUser.setMobileNo(appraiserSaveDto.getAppraiserPhone());
        uacUser.setLoginName(appraiserSaveDto.getLoginName());
        uacUser.setLoginPwd(Md5Util.encrypt("123456")
        );
        uacUser.setStatus(UacUserStatusEnum.ENABLE.getKey());
        uacUser.setRoleCode("business");*/


        //添加评估师
        UacUser uacUser = new UacUser();
        uacUser.setLoginName(appraiserSaveDto.getLoginName());
        UacUser selectOne = userService.selectOne(uacUser);
        UaAppraiser omcAppraiser = new UaAppraiser();
        BeanUtils.copyProperties(appraiserSaveDto,omcAppraiser);
        omcAppraiser.setGroupId(getLoginAuthDto().getGroupId());
        omcAppraiser.setUserId(getLoginAuthDto().getUserId());
        omcAppraiser.setOperator(getLoginAuthDto().getUserName());
        omcAppraiser.setUserIdBind(selectOne.getId());



        int result = appraiserService.save(omcAppraiser);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),result);
    }


    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "删除评估师")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Wrapper addChanner(Long id) {

        int deleteByKey = userService.deleteByKey(id);
        int delete = appraiserService.deleteByKey(id);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),delete);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "修改评估师")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Wrapper update(@RequestBody @Valid AppraiserUpdateDto appraiserUpdateDto, BindingResult br) {

        UacUser uacUser = new UacUser();
        uacUser.setId(appraiserUpdateDto.getId());
        uacUser.setUserName(appraiserUpdateDto.getAppraiserName());
        uacUser.setSex(appraiserUpdateDto.getGender());
        uacUser.setMobileNo(appraiserUpdateDto.getAppraiserPhone());

        int update = userService.update(uacUser);


        UaAppraiser omcAppraiser = new UaAppraiser();
        BeanUtils.copyProperties(appraiserUpdateDto,omcAppraiser);
        omcAppraiser.setUserIdBind(appraiserUpdateDto.getId());

        appraiserService.update(omcAppraiser);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "查询评估师")
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public Wrapper update(@RequestBody AppraiserQueryDto appraiserQueryDto, BindingResult br) {


        appraiserQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        List<AppraiserQueryVo> list=appraiserService.selectQuery(appraiserQueryDto);
        PageInfo<AppraiserQueryVo> pageInfo = new PageInfo<>(list);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    @ValidateAnnotation
    @ApiOperation(httpMethod = "POST", value = "审核评估师")
    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    public Wrapper audit(@RequestBody @Valid AppraiserAuditDto appraiserAuditDto) {

        UaAppraiser omcAppraiser = new UaAppraiser();
        BeanUtils.copyProperties(appraiserAuditDto,omcAppraiser);
        omcAppraiser.setUserIdBind(appraiserAuditDto.getId());
        int update = appraiserService.update(omcAppraiser);


        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),update);
    }
}
